<template>
  <div class="swiper-container" ref="mySwiper">
    <div class="swiper-wrapper">
      <div
        class="swiper-slide"
        v-for="(img, index) in skuImageList.smallImage"
        :key="img.id"
      >
        <img
          @mousemove="changeImg(index)"
          :class="{ active: defaultIndex === index }"
          :src="img.img"
        />
      </div>
    </div>
  </div>
</template>

<script>
import Swiper from "swiper";
export default {
  name: "ImageList",
  props: ["skuImageList"],
  data() {
    return {
      defaultIndex: 0, //参考值
    };
  },
  methods: {
    changeImg(index) {
      // 改变小图边框
      this.defaultIndex = index;
      // 还得改变中图和大图 兄弟关系(全局事件总线)
      this.$bus.$emit("changeImg", index);
    },
  },
  watch: {
    // 修改要监视得数据及ref选中轮播图结构
    skuImageList: {
      handler(newVal) {
        if (!newVal.length) return;
        this.$nextTick(() => {
          new Swiper(this.$refs.mySwiper, {
            slidesPerView: 3, //下面显示几张图
            slidesPerGroup: 2, //一次点击走几张
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          });
        });
      },
      immediate: true,
    },
  },
};
</script>

<style lang="less" scoped>
.swiper-container {
  height: 120px;
  width: 500px;
  box-sizing: border-box;
  padding: 0 12px;

  .swiper-slide {
    width: 90px !important;
    height: 90px;

    img {
      margin-top: 20px;
      width: 100%;
      height: 100%;
      border: 1px solid #ccc;
      padding: 2px;
      width: 65px;
      height: 70px;
      display: block;

      &.active {
        border: 2px solid #f60;
        padding: 1px;
      }

      // &:hover {
      //   border: 2px solid #f60;
      //   padding: 1px;
      // }
    }
  }

  .swiper-button-next {
    left: auto;
    right: 0;
  }

  .swiper-button-prev {
    left: 0;
    right: auto;
  }
}
</style>
